package com.shenbianyun.baiwei;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shenbianyun.bosskgpay.constant.MerInfoConstant;
import com.shenbianyun.bosskgpay.entity.Merchant;
import com.shenbianyun.bosskgpay.entity.RequestMessage;
import com.shenbianyun.bosskgpay.entity.ResponseMessage;
import com.shenbianyun.bosskgpay.entity.sign.UserSignQueryReqModel;
import com.shenbianyun.bosskgpay.service.sign.TestUserSignQueryServiceD;
import com.shenbianyun.bosskgpay.utils.*;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

/**
 * @author ：张一凡
 * @date ：2024年02月23日 17:17:25
 * @description： 签约查询
 */
public class BWUserSignQueryServiceJ extends AbstractJavaSamplerClient {


    public final String CHARSET_UTF_8 = "UTF-8";
    public final long MER_ID = 1676619392976687L;
    public final String MER_PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMhyR4I/yTdDrrI6C7HhHEH20HtbkF+wrFWoG7Rk18TatN7L6rLcW3mV3fPZUM07W7pKEGVz01grrl/OoKHbKWAEm7n+WdRXsBJ+VinVSdI0IynquDjh6qTUM8gBrPaU3KpwSdVqdY51foQw4mr7h93Bq8W6GOkgBiosQYd2yYF5AgMBAAECgYEAk6ZyfEQmIPpPLePKB45tdT2slqF8+K+4UiRRvBDoS45a2x7UVEoza510cBItDfBighfaehDWpkUO3m3jLza0A0PHmbryNsyrw0HSLiPJVaf1/wP2jvI94veXIlL1ttyKsq1kSVhgUuRS2anq/IXdaF2G1NE/rmeGatCPg0kcp7ECQQDinjgD1z8D0y+7duL8HD3+sCL94VXlGrj2DrzI75zXyuf4CBqSSRSX1lfy3HHjw5alVnmUqVQPyIK/P50JWJxlAkEA4m9iy1czgowK7DTUWOGXMVobzZdjvS168FllIJxrj3dhAvoT6MduotUoZ3e+2e9F3AoGiLGh4+NM7lmciNCthQJAb33uCx2z9/rHyhA0nlp6uboIjSV2r+/UveIESUtxu80tER9Ti1L/GbCvtmF0et04WYHQ8S3ZmKZKhTMT3IxG7QJAMoRBGTZaIozVvv7GRRIHJhaBT2uv77ueytJW0mCzSm2KshxSpsMfdqpxeivn4z0Ta9S8wnBGus2yExbg6lEL9QJBAJG8lqFuPfpzHtoCd7Xbv0GWJ+K/modlWPWbxANJQw9OL2sqQ2Was/Ipflyggu+gDDkkhePe77YkSdlAJxVNbzY=";
    public final String MER_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIckeCP8k3Q66yOgux4RxB9tB7W5BfsKxVqBu0ZNfE2rTey+qy3Ft5ld3z2VDNO1u6ShBlc9NYK65fzqCh2ylgBJu5/lnUV7ASflYp1UnSNCMp6rg44eqk1DPIAaz2lNyqcEnVanWOdX6EMOJq+4fdwavFuhjpIAYqLEGHdsmBeQIDAQAB";
    public final String INTER_KEY = "930A7CE165CB2D27F62A34862A397618";
    public final String API_VERSION = "V1.0";
    public final String OMS_TEST_URL = "http://testgateway.serviceshare.com/testapi/clientapi/clientBusiness/common";
    public final String funCode = "6011";
    public final int providerId = 30481;

    public static UserSignQueryReqModel proSignQuery = null;
    public static Merchant mer = null;
    public static TestUserSignQueryServiceD usq = null;

    public String runBWUserSignQueryServiceJ() throws Exception {


        mer = new Merchant();
        /*
         * 商户id
         * */
        mer.setMerid(MER_ID);
        /*
         * 商户私钥
         * */
        mer.setPriKey(MER_PRIVATE_KEY);
        /*
         * 商户公钥
         * */
        mer.setPubKey(MER_PUBLIC_KEY);
        /*
         * 商户签名
         * */
        mer.setInterKey(INTER_KEY);


        //公共参数
        RequestMessage rm = new RequestMessage();
        //接口地址
        rm.setFunCode(funCode);
        rm.setMerId(mer.getMerid());
        rm.setVersion(API_VERSION);
        rm.setReqId(rm.getFunCode() + System.currentTimeMillis());


        //业务参数：自由职业者签约查询参数
        proSignQuery = new UserSignQueryReqModel();
        proSignQuery.setMerUserId("123#=vip-3");
        proSignQuery.setProviderId(providerId);//不是1是小写L
        //reqData转json
        String reqData = JsonUtils.toJson(proSignQuery);
        //System.out.println("reqDatajson= " + reqData);


        //reqData加密
        byte[] bs = DESUtils.encrypt(reqData.getBytes(CHARSET_UTF_8), mer.getInterKey());
        String reqDataEncrypt = Base64Utils.encodeToString(bs);
        rm.setReqData(reqDataEncrypt);
        rm.setSign(RSAUtils.sign(reqDataEncrypt, mer.getPriKey()));

        String reqStr = JsonUtils.toJson(rm);
        //System.out.println("请求数据：" + reqStr);


        String resData = HttpClientHelper.sendPost(OMS_TEST_URL, "application/json", reqStr, "POST", CHARSET_UTF_8);
        //System.out.println("返回数据："+resData);

        //返回信息：DES 解密
        ResponseMessage responseMessage = JSON.parseObject(resData, ResponseMessage.class);
        /** 2、Base64解编码 resDATA对象解密 **/
        String respData = responseMessage.getResData();
        byte[] base64bs = Base64Utils.decode(respData.getBytes());
        /** 3、DES 解密 **/
        String resultData = DESUtils.decryptToString(base64bs, MerInfoConstant.INTER_KEY);
        responseMessage.setResData(resultData);
        //System.out.println("解密结果："+responseMessage);

        return resData;
    }



    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult sampleResult = new SampleResult();
        String resData = "";
        try {
            sampleResult.sampleStart();
            resData = runBWUserSignQueryServiceJ();

            //断言
            JSONObject jsonObject1 = JSONObject.parseObject(resData);
            if (jsonObject1.get("resCode").equals("0000")) {
                sampleResult.setResponseOK();
                sampleResult.setResponseData(true+"", SampleResult.TEXT);
                System.out.println("签约查询结果:" + true);
            } else {
                sampleResult.setSuccessful(false);
                sampleResult.setResponseData(false + " " + jsonObject1.get("resMsg"), SampleResult.TEXT);
                System.out.println("签约查询结果:" + false + " " + jsonObject1.get("resMsg"));
            }
        } catch (Exception e) {
            sampleResult.setSuccessful(false);
            sampleResult.setResponseData(resData, SampleResult.TEXT);
            sampleResult.setResponseMessage(e.toString());

        } finally {
            sampleResult.sampleEnd();
        }
        return sampleResult;
    }


    public static void main(String[] args) {

        new BWUserSignQueryServiceJ().runTest(new JavaSamplerContext(new Arguments()));

    }

}
